/*************************************************************************
** Copyright (C) 2014  Mario Marquez <maqzma@gmail.com>.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/

#include "Dictionary.h"

#include <fstream>

Dictionary::Dictionary(const std::string &filename)
{
    this->load(filename);
}

void Dictionary::load(const std::string &filename)
{
    // Abrirmos el archivo en modo lectura.
    std::ifstream file(filename, std::ios_base::in);

    // Lanzamos una excepcion si algo falla.
    if (file.is_open() != true) {
        std::string error = "Can't load: ";
        error.append(filename);
        throw std::ios_base::failure(error);
    }

    /// Cargamos el nombre desde el archivo.
    std::getline(file, _name);

    /// Cargamos y guardamos las palabras.
    std::string line;

    // Limpiamos para evitar acomulaciones.
    _words.clear();

    // Obtenemos las palabras.
    while (std::getline(file, line))
    {
        _words.push_back(line);
    }

    // Cerramos el archivo.
    file.close();
}

const std::string &Dictionary::name() const
{
    return _name;
}

const Dictionary::WordList &Dictionary::words() const
{
    return _words;
}
